package com.ohos.carbon.provider;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.ComponentViewDataBean;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.List;

public class ComponentViewProvider extends RecyclerAdapter<ComponentViewProvider.ViewHolder> {

    private List<ComponentViewDataBean> mList;
    private Context context;

    public ComponentViewProvider(List<ComponentViewDataBean> mList, Context context) {
        this.mList = mList;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ComponentContainer componentContainer) {

        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_component_view_provider, componentContainer, false);
        return new ViewHolder(component);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        ViewHolder viewHolder = holder;
        if (mList != null ) {
            viewHolder.text1.setText(mList.get(position).getText1());
            viewHolder.text2.setText(mList.get(position).getText2());
            viewHolder.text3.setText(mList.get(position).getText3());
            viewHolder.text3.setTextColor(Color.GRAY);
        }

    }


    @Override
    public Object getItem(int position) {
        if (mList != null && position >= 0 && position < mList.size()) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int positon) {
        return positon;
    }

    class ViewHolder extends RecyclerAdapter.ViewHolder {


        private Text text1,text2,text3;

        public ViewHolder(Component itemComponent) {
            super(itemComponent);
            text1 = (Text) itemComponent.findComponentById(ResourceTable.Id_text1);
            text2 = (Text) itemComponent.findComponentById(ResourceTable.Id_text2);
            text3 = (Text) itemComponent.findComponentById(ResourceTable.Id_text3);
        }
    }
}
